﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AwesomeEnginePC.Graphics.TwoDimensional.Text;
using Microsoft.Xna.Framework.Content;
using AwesomeEnginePC.Physics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using AwesomeEnginePC.Extras;

namespace AwesomeEnginePC.Graphics.TwoDimensional.Sprites
{
    public class BaseSpriteButton : Base2DObject
    {
        private BaseSprite _spriteBackgroud;
        private BaseString _stringForground;

        /// <summary>
        /// Creates a new BaseSpriteButton. spriteAssetName can be left null if the sprite is not desired.
        /// </summary>
        /// <param name="cm"></param>
        /// <param name="stringFontAssetName">The asset name </param>
        /// <param name="spriteAssetName"></param>
        public BaseSpriteButton(ContentManager cm, string stringFontAssetName, string spriteAssetName)
            : base(cm, false)
        {
            _stringForground = new BaseString(cm, stringFontAssetName, "");

            if (!string.IsNullOrWhiteSpace(spriteAssetName))
            {
                _spriteBackgroud = new BaseSprite(cm, spriteAssetName);
                //center string in middle of 
            }

            Scaling = new Vector<float>(1, 1);
            Position = new Vector<int>(0, 0);
            Tint = Color.Orange;

            Active = true;
        }

        public override string ContentAssetName { get { return _stringForground.ContentAssetName; } }

        public override float LayerDepth
        {
            get { return _stringForground.LayerDepth; }
            set
            {
                _stringForground.LayerDepth = value;

                if (_spriteBackgroud != null)
                    _spriteBackgroud.LayerDepth = value;
            }
        }

        public override Vector<int> Position
        {
            get
            {
                if (_spriteBackgroud != null)
                    return _spriteBackgroud.Position;

                return _stringForground.Position;
            }
            set
            {
                if (_spriteBackgroud == null)
                {
                    _stringForground.Position = value;
                }
                else
                {
                    _spriteBackgroud.Position = value;
                    CenterString();
                }
            }
        }

        public override int RawHeight
        {
            get
            {
                if (_spriteBackgroud != null)
                    return _spriteBackgroud.RawHeight;

                return _stringForground.RawHeight;
            }
        }

        public override int RawWidth
        {
            get
            {
                if (_spriteBackgroud != null)
                    return _spriteBackgroud.RawWidth;

                return _stringForground.RawWidth;
            }
        }

        public override float Rotation
        {
            get { return _stringForground.Rotation; }
            set
            {
                _stringForground.Rotation = value;

                if (_spriteBackgroud != null)
                    _spriteBackgroud.Rotation = value;
            }
        }

        public override Vector<float> Scaling
        {
            get { return _stringForground.Scaling; }
            set
            {
                _stringForground.Scaling = value;

                if (_spriteBackgroud != null)
                    _spriteBackgroud.Scaling = value;
            }
        }

        public string Text
        {
            get { return _stringForground.Text; }
            set 
            {
                _stringForground.Text = value;
                if (_spriteBackgroud != null)
                    CenterString();
            }
        }
        /// <summary>
        /// The tint of the foreground text
        /// </summary>
        public override Color Tint
        {
            get { return _stringForground.Tint; }
            set { _stringForground.Tint = value; }
        }

        public override void Update(GameTime gameTime) { }

        /// <summary>
        /// Centers the string in the middle of the background sprite
        /// </summary>
        protected virtual void CenterString()
        {
            if (_spriteBackgroud == null)
                return;

            Vector<int> stringPosBeforeRotation = new Vector<int>();
            stringPosBeforeRotation.X = _spriteBackgroud.Position.X + (_spriteBackgroud.Width / 2) - (_stringForground.Width / 2);
            stringPosBeforeRotation.Y = _spriteBackgroud.Position.Y + (_spriteBackgroud.Height / 2) - (_stringForground.Height / 2);

            _stringForground.Position = stringPosBeforeRotation;
        }

        protected override void DrawInner(GameTime gameTime, SpriteBatch sb)
        {
            if (_spriteBackgroud != null)
                _spriteBackgroud.Draw(gameTime, sb);

            _stringForground.Draw(gameTime, sb);
        }


    }
}
